/*
 * EncounterWatcher.java
 *
 * Created on August 25, 2007, 5:32 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.lct.eq2.data;

/**
 *
 * @author Kris
 */
public class EncounterWatcher extends Thread implements Runnable {
    private boolean encounterActive = false;
    private boolean running = false;
    private static Encounter parentEncounter;
    private static DataHandler parentHandler;
    private int waitTime = 0;
    private int count = 0;
    
    /** Creates a new instance of EncounterWatcher */
    public EncounterWatcher(DataHandler parentHandler, Encounter parentEncounter, int waitTime) {
        this.running = true;
        this.encounterActive = true;
        this.waitTime = waitTime;
        this.parentEncounter = parentEncounter;
        this.parentHandler = parentHandler;
    }
    
    public void refreshEncounter() {
        this.running = true;
        this.count = 0;
        this.encounterActive = true;
    }
    
    public boolean getEncounterActive() {
        return this.encounterActive;
    }
    
    public void setEncounter(Encounter parentEncounter) {
        this.parentEncounter = null;
        this.parentEncounter = parentEncounter;
    }
    
    public void stopRunning() {
        this.running = false;
    }
    
    public void run() {
        //System.out.println("EncounterWatcher::run() - Encounter watcher started at " + new Date());
        this.parentHandler.parent.addPlaceHolder(this.parentEncounter.getMainEnemy());
        while ((count < (waitTime*2)) && (this.running)) {
            try {
                if (this.running) {
                    this.parentHandler.parent.updateEncounterGraph(this.parentEncounter);
                }
                count++;
                this.currentThread().sleep(500);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        if (this.running) {
            this.parentHandler.parent.addEncounter(this.parentEncounter, -1);
        }
        //System.out.println("EncounterWatcher::run() - Encounter watcher ended at   " + new Date());
        this.encounterActive = false;
    }
}
